package com.cskaoyan.service.admin.issue;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.bo.admin.issue.IssueCreateBo;
import com.cskaoyan.bean.bo.admin.issue.IssueListBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Aaliyah
 * @date 2022/9/7 18:09
 * @description: 处理通用问题模块的业务
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    /**
     * @description: 根据条件查询数据库，返回符合条件的问题列表，封装到Vo类中返回
     * @date: 2022/9/7 18:11
     * @param: [issueListBo] 封装了查询条件和分页信息
     */
    @Override
    public CommonData issueList(IssueListBo issueListBo) {

        // 创建example并加入条件
        MarketIssueExample issueExample = new MarketIssueExample();
        issueExample.setOrderByClause(issueListBo.getSort() + " " + issueListBo.getOrder());
        issueExample.setDistinct(true);
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (issueListBo.getQuestion() != null) {
            criteria.andQuestionLike("%" + issueListBo.getQuestion() + "%");
        }

        // 使用example进行查询，使用分页工具进行分页
        PageHelper.startPage(issueListBo.getPage(), issueListBo.getLimit());
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(issueExample);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(marketIssues);

        // 返回Vo
        return CommonData.data(pageInfo);

    }

    /**
     * @description: 将问题和答案存入数据库中，并返回数据库中该条数据完整的数据信息
     * @date: 2022/9/7 20:07
     * @param: [issueCreateBo] 封装了管理员添加的问题和答案
     */
    @Override
    public MarketIssue issueCreate(IssueCreateBo issueCreateBo) {

        // 将Bo内的数据封装进Po中，并将Po补充完整
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(issueCreateBo.getQuestion());
        marketIssue.setAnswer(issueCreateBo.getAnswer());
        Date date = new Date(System.currentTimeMillis());
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);

        // 将Bo中的数据存入数据库
        marketIssueMapper.insert(marketIssue);

        // 通过限定问题和答案，获取该新记录的数据
        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andQuestionEqualTo(issueCreateBo.getQuestion());
        criteria.andAnswerEqualTo(issueCreateBo.getAnswer());
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(issueExample);

        // 如果有还重复问答，返回最新插入的一条
        int index = 0;
        if (marketIssues.size() > 1) {
            index = marketIssues.size() - 1;
        }
        return marketIssues.get(index);
    }

    /**
     * @description: 对目标问答的数据进行修改
     * @date: 2022/9/7 21:53
     * @param: [marketIssue] 封装了修改后的信息的Po
     */
    @Override
    public void issueUpdate(MarketIssue marketIssue){
        // 将addTime设置为null
        marketIssue.setAddTime(null);

        // 将非空数据写入数据库
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
    }

    /**
     * @description: 对数据库中的目标数据进行逻辑删除（将deleted设置为true）
     * @date: 2022/9/7 21:25
     * @param: [marketIssue] 封装了该条问答完整信息的Po
     */
    @Override
    public void issueDelete(MarketIssue marketIssue) {
        marketIssue.setDeleted(true);
        marketIssueMapper.updateByPrimaryKey(marketIssue);
    }
}
